// lib/views/buy_conditions/buy_conditions_view.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../controllers/buy_conditions_controller.dart';
import 'buy_condition_form.dart';
import 'buy_condition_list_item.dart';

class BuyConditionsView extends StatelessWidget {
  final BuyConditionsController controller = Get.put(BuyConditionsController());

  BuyConditionsView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('买入条件管理'),
        actions: [
          IconButton(
            icon: const Icon(Icons.refresh),
            onPressed: controller.loadBuyConditions,
          ),
        ],
      ),
      body: Obx(() {
        if (controller.isLoading.value) {
          return const Center(child: CircularProgressIndicator());
        }

        if (controller.buyConditions.isEmpty) {
          return const Center(
            child: Text('暂无买入条件，点击右下角按钮添加'),
          );
        }

        return ListView.builder(
          itemCount: controller.buyConditions.length,
          itemBuilder: (context, index) {
            final condition = controller.buyConditions[index];
            return BuyConditionListItem(condition: condition);
          },
        );
      }),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          controller.clearEditing();
          Get.to(() => const BuyConditionForm());
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}